<template>
  <div class="pagination-container">
    <el-pagination
      :current-page="localCurrentPage"
      :page-size="localPageSize"
      :page-sizes="[2, 5, 10, 15]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';

// 定义 props
const props = defineProps({
  currentPage: {
    type: Number,
    required: true
  },
  pageSize: {
    type: Number,
    required: true
  },
  total: {
    type: Number,
    required: true
  }
});

// 定义 emits
const emit = defineEmits(['update:currentPage', 'update:pageSize', 'size-change', 'current-change']);

// 本地状态
const localCurrentPage = ref(props.currentPage);
const localPageSize = ref(props.pageSize);

// 监听 props 变化并更新本地状态
watch(() => props.currentPage, (newVal) => {
  localCurrentPage.value = newVal;
});

watch(() => props.pageSize, (newVal) => {
  localPageSize.value = newVal;
});

// 监听本地状态变化并发出事件
watch(localCurrentPage, (newVal) => {
  emit('update:currentPage', newVal);
  emit('current-change', newVal);
});

watch(localPageSize, (newVal) => {
  emit('update:pageSize', newVal);
  emit('size-change', newVal);
});
</script>

<style scoped>
.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-start;
}
</style>
